//
//  DiscoverTableViewController.swift
//  FoodPin
//
//  Created by 王巍 on 16/5/10.
//  Copyright © 2016年 ww. All rights reserved.
//

import UIKit

class DiscoverTableViewController: UITableViewController {
    
    @IBOutlet var spinner: UIActivityIndicatorView!

    var restaurants:[AVObject] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.refreshControl = UIRefreshControl()
        self.refreshControl?.backgroundColor = UIColor.whiteColor()
        self.refreshControl?.tintColor = UIColor.grayColor()
        self.refreshControl?.addTarget(self, action: #selector(DiscoverTableViewController.getNewData), forControlEvents: .ValueChanged)
        
        spinner.hidesWhenStopped = true
        spinner.center = view.center
        view.addSubview(spinner)
        spinner.startAnimating()
        
        getRecordFromCloud()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return restaurants.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
        
        let restaurant = restaurants[indexPath.row]
        cell.textLabel?.text = restaurant["name"] as? String
        
        // 图像占位（默认显示的图片）
        cell.imageView?.image = UIImage(named: "photoalbum")
        
        if let img = restaurant["image"] as? AVFile{
            img.getDataInBackgroundWithBlock({ (data, e) in
                guard e == nil else{
                    print(e.localizedDescription)
                    return
                }
                // 主线程更新图像
                NSOperationQueue.mainQueue().addOperationWithBlock({ 
                    cell.imageView?.image = UIImage(data: data)
                })
            })
        }
        
        /*
        if let img = restaurant["image"] as? AVFile{
            cell.imageView?.image = UIImage(data: img.getData())
        }
        */
        return cell
    }
    
    
    func getNewData(){
        getRecordFromCloud(true)
    }
    
    // 从LeanCloud取出数据
    func getRecordFromCloud(needNew: Bool = false) {
        let query = AVQuery(className: "Restaurant")
        
        query.orderByDescending("createdAt")
        
        // 设置缓存策略
        if needNew{
            query.cachePolicy = .IgnoreCache
        }else{
            query.cachePolicy = .CacheElseNetwork
            query.maxCacheAge = 60 * 2
        }
        
        if query.hasCachedResult(){
            print("从缓存中获取数据")
        }
        
        query.findObjectsInBackgroundWithBlock { (objects, e) in
            // swift 新写法
            guard e == nil else{
                print(e.localizedDescription)
                return
            }
            
            if let objects = objects as? [AVObject]{
                self.restaurants = objects
                //  在主线程中执行代码
                NSOperationQueue.mainQueue().addOperationWithBlock({ 
                    self.tableView.reloadData()
                    self.spinner.stopAnimating()
                    self.refreshControl?.endRefreshing()
                })
            }
            
            /*
            if let e = e {
                print(e.localizedDescription)
            }else if let objects = objects as? [AVObject]{
                self.restaurants = objects
                self.tableView.reloadData()
            }
            */
        }
    }

    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
